#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

bitmq::ExchangeManager::ptr emp;

class ExchangeTest : public testing::Environment
{
    virtual void SetUp() override
    {
        emp = std::make_shared<bitmq::ExchangeManager>("./data/meta.db");
    }

    virtual void TearDown() override
    {
        // emp->clear();
    }
};

TEST(exchange_test, insert_test)
{
    std::unordered_map<std::string, std::string> map;
    emp->declareExchange("ex1", bitmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("ex2", bitmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("ex3", bitmq::ExchangeType::DIRECT, true, false, map);
    emp->declareExchange("ex4", bitmq::ExchangeType::DIRECT, true, false, map);
    ASSERT_EQ(emp->size(), 4);
}

TEST(exchange_test, select_test1)
{
    ASSERT_EQ(emp->exists("ex3"), true);
    ASSERT_EQ(emp->exists("ex1"), true);
    ASSERT_EQ(emp->exists("ex2"), true);
    ASSERT_EQ(emp->exists("ex4"), true);
    bitmq::Exchange::ptr exp = emp->selectExchange("ex2");
    ASSERT_EQ(exp->name, "ex2");
    ASSERT_EQ(exp->durable, true);
    ASSERT_EQ(exp->auto_delete, false);
    ASSERT_EQ(exp->type, bitmq::ExchangeType::DIRECT);
    ASSERT_EQ(exp->getArgs(), std::string("k1=v1&k2=v2&"));
}

TEST(exchange_test, remove_test1)
{
    emp->deleteExchange("ex2");
    bitmq::Exchange::ptr exp = emp->selectExchange("ex2");
    ASSERT_EQ(exp.get(), nullptr);
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    int t = RUN_ALL_TESTS();
    return 0;
}